﻿var zombies = zombies || {};

var dropdownItem = function (data) {
    var self = this;
    self.id = ko.observable(data.id);
    self.name = ko.observable(data.name);
};

zombies.ListOrder = function () {
    var self = this;

    self.OrderDetails = ko.observableArray([]);
    self.OrderLists = ko.observableArray([]);
    self.Status = ko.observable(-1);
    self.Categories = ko.observableArray();
    self.SearchString = ko.observable("2014/01/01");
    self.SearchDate = ko.observable("");
    self.Type = ko.observable(0);
    self.CurrentPage = ko.observable(1);
    self.IsPrevious = ko.observable(false);
    self.IsNext = ko.observable(false);
    self.Loaded = ko.observable(false);
    self.selectedChoice = ko.observable();
    self.Member = ko.observable();
    self.selectionChanged = function (event) {
        load(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.selectedChoice(), self.Type());
        $.replaceUrl(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.selectedChoice(), self.Type());
    }

    self.dropdownItems = ko.observableArray([
        new dropdownItem({ id: "0", name: "Pending" }),
        new dropdownItem({ id: "1", name: "Aproved" }),
        new dropdownItem({ id: "2", name: "Cancel" })
    ]);

    self.SendMsg = function () {
        self.CurrentPage(1);
        self.Type(0);
        load(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.Status(), self.Type());
        $.replaceUrl(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.Status(), self.Type());
    };

    self.SendMsgDate = function () {
        self.CurrentPage(1);
        self.Type(1);
        load(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.Status(), self.Type());
        $.replaceUrl(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.Status(), self.Type());
    };

    self.EnterSendMsg = function (data, event) {
        try {
            if (event.which === 12) {
                self.CurrentPage(1);
                load(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.Status(), self.Type());
                $.replaceUrl(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.Status(), self.Type());
                return false;
            }
            return true;
        }
        catch (e)
        { }
    };


    self.doNext = function () {
        if (self.IsNext()) {
            var page = self.CurrentPage();
            page++;
            self.CurrentPage(page);
            load(page, self.SearchString(), self.SearchDate(), self.Status(), self.Type());
            if (page == 1) {
                self.IsPrevious(false)
            } else {
                self.IsPrevious(true);
            }
            $.replaceUrl(page, self.SearchString(), self.SearchDate(), self.Status(), self.Type());
        }
    };

    self.doPrevious = function () {
        if (self.IsPrevious()) {
            var page = self.CurrentPage() - 1;
            self.CurrentPage(page);
            load(page, self.SearchString(), self.SearchDate(), self.Status(), self.Type());
            if (page == 1) {
                self.IsPrevious(false)
            } else {
                self.IsPrevious(true);
            }
            $.replaceUrl(page, self.SearchString(), self.SearchDate(), self.Status(), self.Type());
        }
    };

    self.doAprroved = function (item) {
        self.OrderData = ko.observable({
            OrderId: item.OrderId,
            Status: 1
        });

        $.ajax({
            type: "PUT",
            url: "/api/AdminOrder/PutOrder",
            data: ko.toJSON(self.OrderData()), //Convert the Observable Data into JSON
            contentType: "application/json",
            dataType: 'json'
        }).done(function () {
            toastr.success('Approved !');
            load(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.Status(), self.Type());
        }).fail(function () {
            toastr.error('Approve Failed !');
        });
    };

    self.doCancel = function (item) {

        self.OrderData = ko.observable({
            OrderId: item.OrderId,
            Status: 2
        });

        $.ajax({
            type: "PUT",
            url: "/api/AdminOrder/PutOrder",
            data: ko.toJSON(self.OrderData()), //Convert the Observable Data into JSON
            contentType: "application/json",
            dataType: 'json'
        }).done(function () {
            toastr.success('Canceled !');
            load(self.CurrentPage(), self.SearchString(), self.SearchDate(), self.Status(), self.Type());
        }).fail(function () {
            toastr.error('Cancel Failed !');
        });
    };

    self.goDetail = function (items) {
        console.log(items);
        self.OrderDetails.removeAll();
        $.each(items.OrderDetail, function (idx, item) {
            self.OrderDetails.push(new zombies.OrderDetail(item));
        });

        $.ajax({
            url: '/api/AdminOrder/GetMember/' + items.UserName,
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        }).done(function (itemRe) {
            self.Member(itemRe);
            self.Member.UserName = items.UserName;
            console.log(self.Member());
        }).error(function () {
            toastr.error('error');
        })
    };
}

var load = function (id, searchString, searchDate, status, type) {
    viewModel.Loaded(false);
    viewModel.OrderLists.removeAll();
    $.ajax({
        url: '/api/AdminOrder/GetOrders/' + id,
        type: 'GET',
        data: { value: searchString, date: searchDate, type: type, status: status },
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        if (items.length === 12) {
            items.pop();
            viewModel.IsNext(true);
        } else {
            viewModel.IsNext(false);
        }
        $.each(items, function (idx, item) {
            viewModel.OrderLists.push(new zombies.Order(item));
        });
        viewModel.Loaded(true);

    }).error(function () {
        toastr.error('error');
    })
};


var viewModel;
$(document).ready(function () {

    viewModel = new zombies.ListOrder();
    ko.applyBindings(viewModel, document.getElementById("admin_content"));

    var pageUrl = $.getUrlVars("page");
    var searchStringUrl = $.getUrlVars("searchString");
    if (typeof pageUrl === 'undefined' || !isFinite(String(pageUrl))) {
        viewModel.CurrentPage(1);
    } else {
        viewModel.CurrentPage(pageUrl);
    }
    if (typeof searchStringUrl === 'undefined') {
        viewModel.SearchString("");
    } else {
        viewModel.SearchString(searchStringUrl);
    }
    if (pageUrl > 1) viewModel.IsPrevious(true);
    load(viewModel.CurrentPage(), viewModel.SearchString(), viewModel.SearchDate(), viewModel.Status, viewModel.Type());
});
